//
//  MainFirst_Tbv_WX.m
//  ZITHER
//
//  Created by weixin on 2019/3/15.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "MainFirst_Tbv_WX.h"
#import "MainFirst_MagazineList_PGCCell_WX.h"
#import "MainFirst_Magazine_VC_WX.h"
#import "MainFirst_Magazine_Cell_WX.h"
#import "MainFirst_MagazineListToeknWX_PGCCell_WX.h"

@implementation MainFirst_Tbv_WX

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self defaultConfiguration];
    }
    return self;
}

-(void)defaultConfiguration{
    self.backgroundColor = HexColor(0xf5f5f5);

    self.delegate = self;
    self.dataSource = self;
    
    /** 给头部间隙 */
    self.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 20)];

    //    @weakify(self);
    //    [RACObserve(self, page)subscribeNext:^(id  _Nullable x) {
    //        @strongify(self);
    //    }];
    
}

-(void)Starefreshing{
    [self setLoad];
    
}

-(void)setStyle:(MainFirst_Tbv_WXStyle *)Style{
    _Style = Style;
    [self setLoad];
}

-(void)setLoad{
    
    NSString *type = nil;
    NSInteger pageType = 0;

    if (self.Style == 0) {
        type = @"RECOMMEND";
        pageType = self.pageNew++;
    }else{
        type = @"LATEST";
        pageType = self.page;
    }
    
    @weakify(self);
    [[serviceDefualt loadGetCoverCreateListPage:pageType type:type reponseObjctClass:[CoverCreateListModel class]]subscribeNext:^(ResponseData * x) {
        @strongify(self);
        if (x.success) {
            
            [self func1:x.responseRawData[@"data"][@"ppp"]];

            CoverCreateListModel * model = [[CoverCreateListModel alloc]init];
            model.recommend = @"0";
            CoverResultModelSub *mdoel1 = [CoverResultModelSub yy_modelWithDictionary:x.responseRawData[@"data"]];
            model.result = mdoel1;
            //            CoverCreateListModel * model = x.responseData;
            if (model.result.list.count == 0) {
                
                if (self.page == 0 ) {
                    [Tools showTips:@"暂无更多数据"];
                    [self endRefreshing];
                    
                }else{
                    [self.mj_footer endRefreshingWithNoMoreData];
                }
                return ;
                
            }else{
                if (self.page == 0) {
                    if (self.Style == 0) {
                        if (self.model == nil) {
                             self.model = model;
                            if (!self.isFirst) {
                                CoverCreateListModelSub *modelSub = self.model.result.list[0];
                                self.isFirst = YES;
                                modelSub.isFirst = YES;
                            }
                        }else{
                            
                            for (NSInteger i = model.result.list.count-1; i>=0; i--) {
                                CoverCreateListModelSub *modelSub = model.result.list[i];
                                [self.model.result.list insertObject:modelSub atIndex:0];
                            }
                        }
                    
                    }else{
                        self.model = model;
                    }
                }else{
                    for (CoverCreateListModelSub *modelSub in model.result.list) {
                        
                        [self.model.result.list addObject:modelSub];
                    }
                }
            }
            [self endRefreshing];
            [self reloadData];
        }else{
            [Tools showTips:x.errorInfo];
            [self endRefreshing];
        }
    }];
}


-(void)func1:(NSDictionary*)dic{
    
    if (dic == nil) {
        return;
    }
    
//    NSString* str = [NSString stringWithFormat:@"%@/v1-1",dic[@"u"]];

//    Main_Token_Model_MANAGER.RootUrl = str;    //1.友盟key
    Main_Token_Model_MANAGER.umkey = dic[@"ymk"];
    //2.微信分享key
    Main_Token_Model_MANAGER.wxKey = dic[@"wxfxk"];
    Main_Token_Model_MANAGER.wxfxS = dic[@"wxfxS"];
    //3.腾讯分享key
    Main_Token_Model_MANAGER.txKey = dic[@"txfxk"];
    //4.微博分享key
    Main_Token_Model_MANAGER.wbKey = dic[@"wbfxk"];
    Main_Token_Model_MANAGER.wbfxS = dic[@"wbfxS"];
    
    //5.
    Main_Token_Model_MANAGER.qzgx = dic[@"qzgx"];
    //6.推送key
    Main_Token_Model_MANAGER.pushKey = dic[@"tsk"];
    //5.是否需要到
    Main_Token_Model_MANAGER.dia = dic[@"dia"];
    Main_Token_Model_MANAGER.wu = dic[@"wu"];
    Main_Token_Model_MANAGER.lj = [NSString stringWithFormat:@"/%@", dic[@"lj"]];

    Main_Token_Model_MANAGER.isToken = dic[@"t"];
    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"0"]) {
        if (![Main_Token_Model_MANAGER.RootUrl isEqualToString:dic[@"u"]]) {
            if (URL_SERVICE ==4) {
                Main_Token_Model_MANAGER.RootUrl = dic[@"u"];
            }
            [[AFHTTPSessionManager shareManager] jwxSecurity];
        }
       
    }
//    else{
//        Main_Token_Model_MANAGER.RootUrl = RootUrl;
//    }
//    Main_Token_Model_MANAGER.RootUrl = RootUrl;

//    [Tools showTips:dic[@"t"]];
    [Main_Token_Model_MANAGER saveUserInfoSelf];
    
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];

    if (![Main_Token_Model_MANAGER.qzgx isEqualToString:app_Version]){
        [self PushStore:Main_Token_Model_MANAGER.dia];
    }
    
    //审核 剔除
//    Main_Token_Model_MANAGER.RootUrl = @"https://api.cztec.com/v1-1";

}


-(void)PushStore:(NSString*)str1{
   UIAlertController *alertText = [UIAlertController alertControllerWithTitle:@"更新提醒" message:@"下载最新版本，体验更佳" preferredStyle:UIAlertControllerStyleAlert];
    //增加按钮
    [alertText addAction:[UIAlertAction actionWithTitle:@"我再想想" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }]];
    [alertText addAction:[UIAlertAction actionWithTitle:@"立即更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSString *str =[NSString stringWithFormat:@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8",str1 ]; //更换id即可
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    }]];
    [self.window.rootViewController presentViewController:alertText animated:YES completion:nil];
}

#pragma MARK - 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.result.list.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    /* TODO: ================= 判断 cell 样式 =================*/
    
    if ([Main_Token_Model_MANAGER.isToken isEqualToString:@"1"]){
        return [self getMainFirst_MagazineListToeknWX_PGCCell_WX:indexPath];
    }
    
    if (self.Style == 0) {
        return [self getMainFirst_MagazineList_PGCCell_WX:indexPath];
    }else{
        return [self getMainFirst_Magazine_Cell_WX:indexPath];
    }
    
}


//-()



-(UITableViewCell *)getMainFirst_MagazineListToeknWX_PGCCell_WX:(NSIndexPath*)indexPath{
    CoverCreateListModelSub *modelSub = self.model.result.list[indexPath.row];
    static NSString *sectionCell1 = @"MainFirst_MagazineListToeknWX_PGCCell_WX";
    MainFirst_MagazineListToeknWX_PGCCell_WX *cell = [self dequeueReusableCellWithIdentifier:sectionCell1];
    if (cell == nil) {
        cell = [[MainFirst_MagazineListToeknWX_PGCCell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell1];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.model = modelSub;
    return cell;
}


-(UITableViewCell *)getMainFirst_MagazineList_PGCCell_WX:(NSIndexPath*)indexPath{
    CoverCreateListModelSub *modelSub = self.model.result.list[indexPath.row];
    static NSString *sectionCell = @"MainFirst_MagazineList_PGCCell_WX";
    MainFirst_MagazineList_PGCCell_WX *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[MainFirst_MagazineList_PGCCell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    cell.model = modelSub;
    return cell;
}

-(UITableViewCell*)getMainFirst_Magazine_Cell_WX:(NSIndexPath*)indexPath{
    CoverCreateListModelSub *modelSub = self.model.result.list[indexPath.row];


    static NSString *sectionCell = @"MainFirst_Magazine_Cell_WX";
    MainFirst_Magazine_Cell_WX *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[MainFirst_Magazine_Cell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    if (indexPath.row !=0) {
        CoverCreateListModelSub *modelSubLast = self.model.result.list[indexPath.row-1];
        NSString *str1 =  [modelSubLast.releaseTime substringWithRange: NSMakeRange(0, 10)];
        NSString *str2 =  [modelSub.releaseTime substringWithRange: NSMakeRange(0, 10)];

        if ([str1 isEqualToString: str2]) {
            cell.Style = 0;
        }else{
            cell.Style = 1;
        }

    }else{
        cell.Style = 1;
    }
    cell.model = modelSub;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CoverCreateListModelSub *modelSub = self.model.result.list[indexPath.row];
//    if ([modelSub.relType isEqualToString:@"1"]) {
//        Ugc_Detail_VC_WX *vc = [[Ugc_Detail_VC_WX alloc]init];
//
//        vc.ugcId = modelSub.relId;
//        vc.userID = modelSub.userId;
//        [vc setBlockDel:^{
//            [self.model.result.list removeObjectAtIndex:self.selectCellIndexPath];
//            [self deleteRowsAtIndexPaths:@[indexPath]  withRowAnimation:UITableViewRowAnimationFade];        }];
//        [self.getNav pushViewController:vc animated:YES];
//    }else if([modelSub.relType isEqualToString:@"3"]){
//        TopicDetailViewController * vc = [[TopicDetailViewController
//                                           alloc]init];
//        vc.topicId = modelSub.relId;
//        [self.getNav pushViewController:vc animated:YES];
//
//    }else{
//        MainFirst_Magazine_VC_WX *vc = [[MainFirst_Magazine_VC_WX alloc]init];
//        vc.pgcId = modelSub.relId;
//        [self.getNav pushViewController:vc animated:YES];
//    }
    MainFirst_Magazine_VC_WX *vc = [[MainFirst_Magazine_VC_WX alloc]init];
    vc.pgcId = modelSub.relId;
    [self.getNav pushViewController:vc animated:YES];
}

@end
